package xyz.dava.sync.fm.vo;

import cn.hutool.core.io.FileUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Data
@Document(collection = "file")
@org.springframework.data.elasticsearch.annotations.Document(indexName = "file")
public class FileTO {

    @Id
    private String id;

    private Boolean isFile;
    private Boolean isRootFile;
    private String fileName;
    private String fileNameNoExt;
    private String folder;
    private String fullPath;
    /**
     * 1:新版
     * 2:旧版
     */
    private Integer fileVersion;
    private FileTO attachment;
    private String uploadCode;
    private String uploaderEmployeeId;
    private String uploaderName;
    private Long orders;
    private FileInfo info;
    private List<RangeVO> ranges;

    private Integer delFlag;
    private String createdBy;
    private String createdDate;
    private String updatedBy;
    private String updatedDate;

    public FileTO() {
        setRanges(new ArrayList<>());
    }

    @Data
    public static class FileInfo {
        private Long checksumCRC32;
        private String fileMine;
        private String fileExt;
        private Long fileSize;

        public FileInfo() {
        }

        public FileInfo(File file) {
            try {
                this.setChecksumCRC32(FileUtil.checksumCRC32(file));
                this.setFileSize(FileUtil.size(file));
            }catch (Exception ignore){
            }
            this.setFileExt(FileUtil.extName(file));
            this.setFileMine(FileUtil.getMimeType(file.getName()));
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RangeVO {
        private String type;
        private String dataId;
    }
}
